package util;

import java.sql.*;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public final class ConnectionDataSource {
  /** Uses DriverManager.  
 * @throws NamingException 
 * @throws SQLException */
 public java.sql.Connection getSimpleConnection() throws SQLException {
    //See your driver documentation for the proper format of this string :
	//jdbc/log720_20143_6
    String DB_CONN_STRING = "jdbc/log720_20143_6";
    //Provided by your driver documentation. In this case, a MySql driver is used : 
    String USER_NAME = "aj95460";
    String PASSWORD = "Log7203";
    Connection conn = null;
    Connection result = null;   
    try {
    	Class.forName("org.postgresql.Driver");
    	Context initContext = new InitialContext();
    	Context envContext  = (Context)initContext.lookup("java:/comp/env");
    	DataSource ds = (DataSource)envContext.lookup(DB_CONN_STRING);
    	result = ds.getConnection();
    }
    catch (SQLException e){
    	System.out.println("Driver loaded, but cannot connect to db: " + DB_CONN_STRING);
    	e.printStackTrace();
    	throw e;
    } 
    catch (ClassNotFoundException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
    } 
    catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}  
	return result;
 }
}